package cn.hzking.gjmall.activitys.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.hzking.gjmall.R;
import cn.hzking.gjmall.activitys.MainActivity;
import cn.hzking.gjmall.app.Gjmall;
import cn.hzking.gjmall.base.ColorStatuBarActivity;
import cn.hzking.gjmall.bean.WxLoginBean;
import cn.hzking.gjmall.net.RetrofitFactory;
import cn.hzking.gjmall.net.base.BaseEntity;
import cn.hzking.gjmall.net.base.BaseObserver;
import cn.hzking.gjmall.utils.OnClickEvent;
import cn.hzking.gjmall.utils.SpPutUtils;
import cn.hzking.gjmall.utils.UIUtils;

public class RegistActivity extends ColorStatuBarActivity {

    @Bind(R.id.tv_regist)
    TextView tvRegist;
    @Bind(R.id.et_phone)
    EditText etPhone;
    @Bind(R.id.et_yanzhen)
    EditText etYanzhen;
    @Bind(R.id.tv_getyanzhen)
    TextView tvGetyanzhen;
    @Bind(R.id.btn_login)
    TextView btnLogin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone_login);
        ButterKnife.bind(this);
        initListener();
    }

    private void initListener() {
        //new倒计时对象,总共的时间,每隔多少秒更新一次时间
        final MyCountDownTimer myCountDownTimer = new MyCountDownTimer(60000,1000);
        //得到验证码
        tvGetyanzhen.setOnClickListener(new OnClickEvent() {
            @Override
            public void singleClick(View v) {
                String num = etPhone.getText().toString().trim();
                boolean mobileNO = UIUtils.isMobileNO(num);
                if(mobileNO) {
                    myCountDownTimer.start();
                    getCode(num,myCountDownTimer);
                }else {
                    UIUtils.showToast(RegistActivity.this,"手机号码不正确", Toast.LENGTH_SHORT);
                }


            }
        });
        //登录
        btnLogin.setOnClickListener(new OnClickEvent() {
            @Override
            public void singleClick(View v) {
                String num = etPhone.getText().toString().trim();
                String code = etYanzhen.getText().toString().trim();
                int length = code.length();
                boolean mobileNO = UIUtils.isMobileNO(num);
                if(mobileNO && length== 6) {
                    regist(num,code);
                }else {
                    UIUtils.showToast(RegistActivity.this,"手机号或验证码不正确", Toast.LENGTH_SHORT);
                }


            }
        });
    }

    //获得验证码
    private void getCode(String phoneNum, final MyCountDownTimer myCountDownTimer) {
        RetrofitFactory.getInstant().API()
                .getYanZhenCode(phoneNum)
                .compose(this.<BaseEntity>setThread())
                .subscribe(new BaseObserver<BaseEntity>(this,false,true) {
                    @Override
                    protected void onSuccees(BaseEntity baseEntity) throws Exception {
                        Gjmall.getHandler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                myCountDownTimer.onFinish();
                                myCountDownTimer.cancel();
                            }
                        },5000);

                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {

                    }

                    @Override
                    protected void onCodeError(BaseEntity baseEntity) throws Exception {
                        super.onCodeError(baseEntity);
                        UIUtils.showToast(RegistActivity.this,baseEntity.getErrorMsg(),Toast.LENGTH_SHORT);
                    }
                });
    }

    //注册并登录
    private void regist(String num,String code) {
        RetrofitFactory.getInstant().API()
                .registAndLogin(num,code)
                .compose(this.<WxLoginBean>setThread())
                .subscribe(new BaseObserver<WxLoginBean>(this,true,true) {
                    @Override
                    protected void onSuccees(WxLoginBean loginBean) throws Exception {
                        SpPutUtils.put(RegistActivity.this,loginBean);
                        Intent intent = new Intent(RegistActivity.this, MainActivity.class);
                        startActivity(intent);
                        finish();


                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        UIUtils.showToast(RegistActivity.this,getResources().getString(R.string.request_error),Toast.LENGTH_SHORT);
                    }

                    @Override
                    protected void onCodeError(WxLoginBean loginBean) throws Exception {
                        super.onCodeError(loginBean);
                        UIUtils.showToast(RegistActivity.this,loginBean.getErrorMsg(),Toast.LENGTH_SHORT);

                    }
                });
    }


    //复写倒计时
    private class MyCountDownTimer extends CountDownTimer {

        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        //计时过程
        @Override
        public void onTick(long l) {
            //防止计时过程中重复点击
            tvGetyanzhen.setClickable(false);
            tvGetyanzhen.setText(l/1000+"秒");

        }

        //计时完毕的方法
        @Override
        public void onFinish() {
            //重新给Button设置文字
            tvGetyanzhen.setText("获取验证码");
            //设置可点击
            tvGetyanzhen.setClickable(true);
        }
    }
}
